package com.ruoyi.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.hospital.domain.Departments;
import com.ruoyi.hospital.domain.Hospitals;
import com.ruoyi.hospital.mapper.DepartmentsMapper;
import com.ruoyi.hospital.service.DepartmentsService;
import com.ruoyi.hospital.service.HospitalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentsServiceImpl extends ServiceImpl<DepartmentsMapper, Departments> implements DepartmentsService {

    @Autowired
    private HospitalsService hospitalsService;

    @Override
    public List<Departments> selectDepartmentsList(Departments departments){

        ArrayList<Departments> departmentsList=new ArrayList<>();
        if(departments.getDeptName()== null){
            List<Departments> list=super.list();
            setList(departmentsList,list);
            return departmentsList;
        }


        String deptName=departments.getDeptName();
        LambdaQueryWrapper<Departments> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Departments::getDeptName,deptName);
        List<Departments> list=super.list(lambdaQueryWrapper);
        setList(departmentsList,list);
        return departmentsList;

    }




    //获取医院的名称并将其封装到科室中
    public void setList(ArrayList<Departments> departmentsList,List<Departments> list){
        list.forEach(department->{
            //获取医院的id
            String hospitalId=department.getHospitalId();
            Hospitals hospitals=hospitalsService.getById(hospitalId);
            department.setHospitalName(hospitals.getHospitalName());
            departmentsList.add(department);
        });


    }
    @Override
    public Departments selectDepartmentsById(Long deptId){
        return super.getOne(new QueryWrapper<Departments>().eq("dept_id",deptId));
    }

    @Override
    public boolean insertDepartments(Departments departments){
        return super.save(departments);
    }

    @Override
    public boolean updateDepartments(Departments departments){
        super.update(departments,new QueryWrapper<Departments>().eq("dept_id",departments.getDeptId()));
        return true;

    }
    @Override
    public boolean deleteDepartmentsByDeptIds( List<Long> deptIds){
        QueryWrapper<Departments> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("dept_id",deptIds);
        return super.remove(queryWrapper);
    }

    @Override
    public List<Departments> getAllDepartments(String hospitalName){
        //通过hospitalName 查询医院信息
        QueryWrapper<Hospitals> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("hospital_name",hospitalName);
        Hospitals hospitals=hospitalsService.getOne(queryWrapper);
        
        if (hospitals == null) {
            return new ArrayList<>();
        }
        
        //通过医院id查询医院科室
        QueryWrapper<Departments> queryWrapperD=new QueryWrapper<>();
        queryWrapperD.eq("hospital_id",hospitals.getHospitalId());
        List<Departments> departmentsList = super.list(queryWrapperD);
        
        //设置医院名称到科室对象中
        departmentsList.forEach(department -> {
            department.setHospitalName(hospitals.getHospitalName());
        });
        
        return departmentsList;
    }

    /**
     * 通过医院名称查询科室列表（支持模糊查询）
     * @param hospitalName 医院名称
     * @return 科室列表
     */
    @Override
    public List<Departments> selectDepartmentsByHospitalName(String hospitalName) {
        // 通过医院名称模糊查询医院信息
        QueryWrapper<Hospitals> hospitalQueryWrapper = new QueryWrapper<>();
        hospitalQueryWrapper.like("hospital_name", hospitalName);
        List<Hospitals> hospitalsList = hospitalsService.list(hospitalQueryWrapper);
        
        if (hospitalsList.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 获取所有匹配医院的ID
        List<String> hospitalIds = new ArrayList<>();
        hospitalsList.forEach(hospital -> hospitalIds.add(hospital.getHospitalId()));
        
        // 通过医院ID列表查询科室
        QueryWrapper<Departments> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.in("hospital_id", hospitalIds);
        List<Departments> departmentsList = super.list(deptQueryWrapper);
        
        // 设置医院名称到科室对象中
        departmentsList.forEach(department -> {
            String hospitalId = department.getHospitalId();
            Hospitals hospital = hospitalsList.stream()
                    .filter(h -> h.getHospitalId().equals(hospitalId))
                    .findFirst()
                    .orElse(null);
            if (hospital != null) {
                department.setHospitalName(hospital.getHospitalName());
            }
        });
        
        return departmentsList;
    }
}
